<template>
<el-dialog title="添加联系方式" :visible.sync="visible" append-to-body width="400px">
    <el-form :model="ruleForm" label-width="80px" label-position="left">
        <el-form-item label="角色">
            <el-select v-model="ruleForm.role" placeholder="请选择" class="w100">
                <el-option v-for="(item,key) in roleTypes" :key="key" :label="item.title" :value="item.value"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="类型">
            <el-select v-model="ruleForm.contact_type" placeholder="请选择" class="w100">
                <el-option v-for="(item,key) in contactTypes" :key="key" :label="item.title" :value="item.value"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item :label="contactLabel">
            <el-input v-model="ruleForm.contact" autocomplete="off"></el-input>
            <el-alert v-if="ruleForm.contact_type == 3" title="例: 021-1234567,0755-12345678-123" type="info" :closable="false" style="margin-top: 5px;">
            </el-alert>
        </el-form-item>
        <el-form-item label="默认">
            <el-select v-model="ruleForm.is_default" placeholder="请选择" class="w100">
                <el-option label="否" :value="0"></el-option>
                <el-option label="是" :value="1"></el-option>
            </el-select>
        </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="appendContact">确 定</el-button>
    </div>
</el-dialog>
</template>

<script>
import {
    checkContactUniqueApi
} from '@/api/customer/contact';

import {
    contactTypes,
    roleTypes
} from '@/config/reflection.js';

const ruleForm = {
    contact_type: "1",
    role: "1",
    contact: "",
    is_default: 0
};

export default {
    name: "AppendContact",
    props: {
        contacts: {
            type: Array,
            default: []
        }
    },
    data() {
        return {
            visible: false,
            contactTypes: contactTypes,
            roleTypes: roleTypes,
            ruleForm: Object.assign({}, ruleForm)
        };
    },
    computed: {
        contactLabel() {
            const contactType = this.contactTypes.find(item => item.value === this.ruleForm.contact_type);
            return contactType ? contactType.title : '';
        },
    },
    methods: {
        showDialog() {
            this.ruleForm = Object.assign({}, ruleForm);
            this.visible = true;
        },
        closeDialog() {
            this.visible = false;
        },
        // 添加联系方式
        appendContact() {
            let _this = this;
            let contactType = contactTypes.find(ele => ele.value == this.ruleForm.contact_type);
            if (!contactType.pattern.test(this.ruleForm.contact)) {
                this.$message.error(contactType.err);
                return false;
            }

            // 先进行当前表单联系方式的检查
            try {
                this.contacts.map(ele => {
                    if (ele.contact == this.ruleForm.contact) {
                        throw "请勿重复添加";
                    }
                })
            } catch (err) {
                this.$message.error(err);
                return false;
            }

            // 系统联系方式唯一性检查
            checkContactUniqueApi({
                keyword: this.ruleForm.contact
            }).then(res => {
                if (res.code == '200') {
                    let row = res.data.row;
                    if (Object.keys(row).length == 0) {
                        _this.contacts.push(this.ruleForm);
                        _this.closeDialog();
                    } else {
                        _this.$message("该联系方式已存在");
                    }
                } else {
                    _this.$message({
                        type: "error",
                        message: res.msg
                    })
                }
            })
        }
    }
}
</script>

<style lang="scss" scoped>
.w100 {
    width: 100%;
}
</style>
